package com.ruoyi.web.controller.manage;

import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginBody;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.manage.domain.request.EncryptedRequest;
import com.ruoyi.manage.domain.request.LoginRequest;
import com.ruoyi.manage.service.AppLoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @Author: wwy
 * @Date: 2020/12/29 14:57
 */
@Api("移动端登录")
@RestController
@RequestMapping("/api")
public class AppLoginController extends BaseController {
    @Autowired
    private AppLoginService appLoginService;
    @Autowired
    private TokenService tokenService;
    /**
     *
     * @param code
     * @return
     */
    @ApiImplicitParam(name = "code", value = "微信code", required = true, dataType = "String", paramType = "path")
    @GetMapping("/login/{code}")
    public AjaxResult login(@PathVariable("code") String code){
        AjaxResult login = appLoginService.login(code);
        Map<String,Object> data = (Map<String,Object>)login.get("data");
        int code1 = (Integer)(login.get("code"));
        if(code1 == HttpStatus.REAL_NAME_AUTHENTICATION){
            return login;
        }
        String openId = String.valueOf(data.get("openId"));
        //生成token
        LoginUser loginUser = new LoginUser();
        loginUser.setOpenId(openId);
        SysUser sysUser = new SysUser();
        sysUser.setUserName("app");
        sysUser.setPassword("app");
        loginUser.setUser(sysUser);
        String token = tokenService.createToken(loginUser);
        LoginRequest loginRequest = new LoginRequest();
        loginRequest.setOpenId(openId);
        loginRequest.setToken(token);
        return AjaxResult.success(loginRequest);
    }

    /**
     * 获取手机号
     * @return
     */
    @ApiOperation("获取手机号")
    @PostMapping("/getPhone")
    public AjaxResult getPhone(@RequestBody EncryptedRequest encryptedRequest){
        return appLoginService.getPhone(encryptedRequest);
    }

    /**
     * 解除绑定
     * @param request
     * @return
     */
    @ApiOperation("解绑")
    @PostMapping("/removeBinding")
    public AjaxResult removeBinding(HttpServletRequest request){
        LoginUser loginUser = tokenService.getLoginUser(request);
        String openId = loginUser.getOpenId();
        return appLoginService.removeBinding(openId);
    }

    /**
     * 获取用户信息
     * @return
     */
    @ApiOperation("获取用户信息")
    @GetMapping("/getUserInfo")
    public AjaxResult getUserInfo(HttpServletRequest request){
        LoginUser loginUser = tokenService.getLoginUser(request);
        String openId = loginUser.getOpenId();
        return appLoginService.getUserInfo(openId);
    }
}
